#include "client.h"
#include "trans_file.h"
#include "child_pthread.h"

int main(int argc,char** argv)
{
    ARGS_CHECK(argc,3);
    int sfd;
    sfd=socket(AF_INET,SOCK_STREAM,0);//初始化一个网络描述符，对应了一个缓冲区
    ERROR_CHECK(sfd,-1,"socket");
    printf("sfd=%d\n",sfd);
    struct sockaddr_in ser_addr;
    bzero(&ser_addr,sizeof(ser_addr));//清空
    ser_addr.sin_family=AF_INET;//代表要进行ipv4通信
    ser_addr.sin_addr.s_addr=inet_addr(argv[1]);//把ip的点分十进制转为网络字节序
    ser_addr.sin_port=htons(atoi(argv[2]));//把端口转为网络字节序
    //客户端就要去连接服务器
    int ret=connect(sfd,(struct sockaddr *)&ser_addr,sizeof(ser_addr));
    ERROR_CHECK(ret,-1,"connect");
    //创建子线程，用于上传下载任务
    Thread_Info_t ti;
    memcpy(&(ti.ser_addr),&ser_addr,sizeof(ser_addr));
    ti.len_of_ser_addr=sizeof(ser_addr);
    ti.notification=0;
    pthread_t pthid;
    child_pthread_create(&pthid,&ti);
    int read_len;
    int recv_len;
    int send_len;
                               
    //char buf[100]={0};
    train_state_t t;
    printf("hello please select register or log in\n");
    printf("1 means register,2 means log in,please press\n");
    char user_choose;
    scanf("%c",&user_choose);
    if(user_choose=='1'){
        bzero(&t,sizeof(train_state_t));
        t.state=REGISTER;
        printf("UserName:\n");
        scanf("%s",t.buf);
        t.data_len=4+strlen(t.buf);
        send_n(sfd,&t.data_len,4);
        send_n(sfd,&t.state,t.data_len);

        bzero(&t,sizeof(train_state_t));
        t.state=REGISTER;
        printf("Password:\n");
        scanf("%s",t.buf);
        t.data_len=4+strlen(t.buf);
        send_n(sfd,&t.data_len,4);
        send_n(sfd,&t.state,t.data_len);
    }else if(user_choose=='2'){
TAG1:   
        bzero(&t,sizeof(train_state_t));
        t.state=LOGIN;
        printf("UserName:\n");
        scanf("%s",t.buf);
        t.data_len=4+strlen(t.buf);
        send_n(sfd,&t.data_len,4);
        send_n(sfd,&t.state,t.data_len);

        bzero(&t,sizeof(train_state_t));
        t.state=LOGIN;
        printf("Password:\n");
        scanf("%s",t.buf);
        t.data_len=4+strlen(t.buf);
        send_n(sfd,&t.data_len,4);
        send_n(sfd,&t.state,t.data_len);
    }else{
        close(sfd);
        return 0;
    }
    //发送完用户名或者密码，等待客户端的反馈，若返回token，说明注册或者登录成功，若返回loginfail，说明登录失败
    bzero(&t,sizeof(train_state_t));
    recv_n(sfd,&t.data_len,4);
    recv_n(sfd,&t.state,t.data_len);
    if(t.state==TOKEN){
        printf("success login!\n");
        bzero(ti.token,25);
        strcpy(ti.token,t.buf);
        ti.notification=1;
    }else if(t.state==LOGINFAIL){ 
        printf("error username or password,input again\n");              //如果密码或者用户名错误，重新输入
        goto TAG1;
    }
    //完成登录,开始给服务器端发命令，ctrl+c则退出
    char cmd[128];
    char recv_result[100];
	while(bzero(cmd, 128),bzero(recv_result,100),(read_len=read(0,cmd,128))>0){
        printf("read_len=%d\n",read_len);//
        trim_space(cmd);
        printf("cmd=%s\n",cmd);
		if(strncmp("cd", cmd, 2) == 0){
            send_session(sfd,&t,CD,cmd+3);
			recv_result_from_server(sfd,&recv_len,recv_result);
		}else if(strncmp("ls", cmd, 2) == 0){
			send_session(sfd,&t,LS,cmd+2);
            recv_result_from_server(sfd,&recv_len,recv_result);
		}else if( strncmp("remove", cmd, 6) == 0){
			send_session(sfd,&t,REMOVE,cmd+7);
			recv_result_from_server(sfd,&recv_len,recv_result);
		}else if(strncmp("pwd", cmd, 3) == 0){
			send_session(sfd,&t,PWD,cmd+3);
			recv_result_from_server(sfd,&recv_len,recv_result);
        }else if(strncmp("puts", cmd, 4) == 0){
            bzero(ti.command,200);
            strcpy(ti.command,cmd);
            ti.notification=1;
		}else if( strncmp("gets", cmd, 4)== 0){
            bzero(ti.command,200);
            strcpy(ti.command,cmd);
            ti.notification=1;
		}else {
			continue ;
		}
    }
    close(sfd);
}
